package com.ntschy.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ntschy.common.core.entity.dataobject.SysDictData;
import com.ntschy.common.core.entity.dataobject.SysDictType;
import com.ntschy.common.core.entity.enums.SysWhetherEnum;
import com.ntschy.common.utils.DictUtils;
import com.ntschy.sys.mapper.SysDictDataMapper;
import com.ntschy.sys.mapper.SysDictTypeMapper;
import com.ntschy.sys.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author ezreal_geng
 */
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init() {
        List<SysDictType> dictTypeList = dictTypeMapper.selectList(null);
        for (SysDictType dictType : dictTypeList) {
            List<SysDictData> dictDataList = dictDataMapper.selectList(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, dictType.getDictType()));
            DictUtils.setDictCache(dictType.getDictType(), dictDataList);
        }
    }

    // * ================================================基础增删改查BEGIN============================================= *

    /**
     * 分页查询字典类型
     * 当前查询条件：字典名称 ｜ 字典类型
     * @param dictType 字典类型对象
     * @return 字典类型对象列表
     */
    @Override
    public List<SysDictType> pageDictType(SysDictType dictType) {
        return dictTypeMapper.pageDictType(dictType);
    }

    /**
     * 新增字典类型
     * @param dictType
     * @return
     */
    @Override
    public int addDictType(SysDictType dictType) {
        dictType.setDictId(IdUtil.fastUUID());
        int row = dictTypeMapper.insert(dictType);
        if(row > 0) {
            DictUtils.clearDictCacheAll();
        }
        return row;
    }

    /**
     * 根据ID更新字典类型
     * @param dictType 字典类型对象
     * @return 更新结果
     */
    @Override
    public int updateDictType(SysDictType dictType) {
        int row = dictTypeMapper.updateById(dictType);
        if(row > 0) {
            DictUtils.clearDictCacheAll();
        }
        return row;
    }

    /**
     * 根据ID删除字典类型
     * @param dictId 字典类型
     * @return 删除结果
     */
    @Override
    public int deleteDictType(String dictId) {
        return dictTypeMapper.deleteById(dictId);
    }

    // * =================================================基础增删改查END============================================== *


    @Override
    public List<SysDictData> listDictDataByDictType(String dictType) {
        List<SysDictData> dictDataList = DictUtils.getDictCache(dictType);
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        dictDataList = dictDataMapper.selectList(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, dictType));
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            DictUtils.setDictCache(dictType, dictDataList);
            return dictDataList;
        }
        return null;
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCacheAll();
    }

    /**
     * 校验字典名称是否存在
     * @param dictName 字典名称
     * @return 校验结果
     */
    @Override
    public boolean checkExistDictName(String dictName) {
        return dictTypeMapper.selectCount(new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getDictName, dictName)) > 0;
    }

    /**
     * 校验字典类型是否存在
     * @param dictType 字典类型
     * @return 校验结果
     */
    @Override
    public boolean checkExistDictType(String dictType) {
        return dictTypeMapper.selectCount(new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getDictType, dictType)) > 0;
    }

    /**
     * 校验字典类型是否系统内置
     * @param dictId 字典类型id
     * @return 校验结果
     */
    @Override
    public boolean checkDictTypeIsSystem(String dictId) {
        SysDictType dictType = dictTypeMapper.selectById(dictId);
        if(ObjectUtil.isNull(dictType)) {
            return false;
        } else {
            return dictType.getIsSystem() == SysWhetherEnum.IS;
        }
    }

    /**
     * 校验字典类型是否存在字典数据
     * @param dictId 字典类型id
     * @return 校验结果
     */
    @Override
    public boolean checkExistDictData(String dictId) {
        SysDictType dictType = dictTypeMapper.selectById(dictId);
        if(ObjectUtil.isNull(dictType)) {
            return false;
        } else {
            Integer i = dictDataMapper.selectCount(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, dictType.getDictType()));
            return i > 0;
        }

    }
}
